package com.sgzn.sgpay.service.impl;

import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.model.request.SgsPrivAddRequest;
import com.sgzn.sgpay.model.request.SgsPrivDeleteRequest;
import com.sgzn.sgpay.model.request.SgsPrivUpdateRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.persistence.dao.TPrivilegeMapper;
import com.sgzn.sgpay.persistence.domain.TPrivilege;
import com.sgzn.sgpay.persistence.domain.TPrivilegeExample;
import com.sgzn.sgpay.service.SgsPrivilegeManageService;
import com.sgzn.sgpay.service.converter.PrivReqConverter;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 权限管理服务
 * @author zhongzhongyong
 * @version Id: SgsPrivilegeManageServiceImpl.java, v 0.1 2018/5/1 13:59 zhongzhongyong Exp $$
 */
@Slf4j
@Service
public class SgsPrivilegeManageServiceImpl implements SgsPrivilegeManageService {

    @Autowired
    private TPrivilegeMapper tPrivilegeMapper;

    @Override
    public SgsResultResponse<String> add(SgsPrivAddRequest request) {
        try{
            TPrivilege tPrivilege = PrivReqConverter.addReqConvert(request);
            int result = tPrivilegeMapper.insertSelective(tPrivilege);
            if(result == 0){
                throw new Exception("TPrivilegeMapper add  Exception!");
            }
            //返回权限id
            return SgsResponseUtil.INSTANCE.success(tPrivilege.getPrivId());
        }catch (Exception e){
            log.error(" 新增权限异常：",e);
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_DB_EXCEPTION.getResponseCode(),
                    SgsResponseCode.C_DB_EXCEPTION.getResponseDesc());
        }
    }

    @Override
    public SgsResultResponse<String> update(SgsPrivUpdateRequest request) {
        try{
            TPrivilege tPrivilege = PrivReqConverter.updateReqConvert(request);
            TPrivilegeExample example = new TPrivilegeExample();
            TPrivilegeExample.Criteria cra = example.createCriteria();
            cra.andPrivIdEqualTo(request.getPrivId());
            int result = tPrivilegeMapper.updateByExampleSelective(tPrivilege,example);
            if(result == 0){
                throw new Exception("TPrivilegeMapper update Exception!");
            }
            //返回权限id
            return SgsResponseUtil.INSTANCE.success(tPrivilege.getPrivId());
        }catch (Exception e){
            log.error(" 更新权限异常：",e);
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_DB_EXCEPTION.getResponseCode(),
                    SgsResponseCode.C_DB_EXCEPTION.getResponseDesc());
        }
    }

    @Override
    public SgsResultResponse<String> delete(SgsPrivDeleteRequest request) {
        try{
            TPrivilegeExample example = new TPrivilegeExample();
            TPrivilegeExample.Criteria cra = example.createCriteria();
            cra.andPrivIdEqualTo(request.getPrivId());
            int result = tPrivilegeMapper.deleteByExample(example);
            if(result == 0){
                throw new Exception("TPrivilegeMapper delete Exception!");
            }
            //返回成功
            return SgsResponseUtil.INSTANCE.success("success");
        }catch (Exception e){
            log.error(" 删除权限异常：",e);
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_DB_EXCEPTION.getResponseCode(),
                    SgsResponseCode.C_DB_EXCEPTION.getResponseDesc());
        }
    }

}
